package com.xbongbong.saas.help;

import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.analytical.SaasValidateAnalyticalService;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowTransferDataEntity;
import com.xbongbong.workflow.model.WorkflowTransferDataModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/20 9:51
 */
@Component
public class AnalyticalHelp {


    @Resource
    private Map<Integer, SaasValidateAnalyticalService> analyticalServiceMap;
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private WorkflowTransferDataModel workflowTransferDataModel;

    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), validateDataDTO.getSaasMark())) {
            SaasValidateAnalyticalService saasValidateAnalyticalService = analyticalServiceMap.get(validateDataDTO.getBusinessType());
            saasValidateAnalyticalService.analyticalData(validateDataDTO);
        } else {
            SaasValidateAnalyticalService saasValidateAnalyticalService = analyticalServiceMap.get(XbbRefTypeEnum.PAAS.getCode());
            saasValidateAnalyticalService.analyticalData(validateDataDTO);
        }
    }

    public boolean isNew(Long dataId, Long taskId, String corpid) {
        boolean isNew = Objects.isNull(dataId) || Objects.equals(dataId,0L);
        if (Objects.nonNull(taskId) && commonHelp.isOpenWorkFlow(corpid)) {
            WorkflowTransferDataEntity workflowTransferDataEntity = workflowTransferDataModel.getByTaskId(taskId, corpid);
            isNew = Objects.equals(1, workflowTransferDataEntity.getOperate()) || isNew;
        }
        return isNew;
    }
}
